package com.hz.springboot02.controller;

import com.hz.springboot02.pojo.Role;
import com.hz.springboot02.pojo.User;
import com.hz.springboot02.service.UserService;
import com.hz.springboot02.utils.MessageJson;
import com.hz.springboot02.utils.ResultLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserController
 * @Description 用户接口
 * @Author Administrator
 * @Date 2024/11/26 16:59
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/userLogin")
    public MessageJson<User> userLogin(String username, String password, String remember, HttpSession session, HttpServletResponse response) {
        User user = userService.login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(60);//设置session保存信息的最大时间
            if (remember != null ){  //判断用户是否存在
                String sName = null;//中文转码
                try {
                    sName = URLEncoder.encode(username,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                Cookie cookie = new Cookie("uname",sName);
                Cookie cookie1 = new Cookie("upwd",password);
                cookie.setMaxAge(60);
                cookie1.setMaxAge(60);
                cookie.setPath("/");
                cookie1.setPath("/");
                response.addCookie(cookie);                   //将用户名和密码存入Cookie
                response.addCookie(cookie1);
            }
            return MessageJson.success(user);
        }else{
            return MessageJson.error(user);
        }
    }

    @GetMapping("/userIndex")
    public ResultLayUi userIndex(Integer page, Integer limit, String userName, String address, String beginDate, String endDate, String roleName){
        System.out.println(userService.listPage(page,limit,userName,roleName,address,beginDate,endDate));
        return userService.listPage(page,limit,userName,roleName,address,beginDate,endDate);
    }
    @PostMapping("/addOrEditUser")
    public MessageJson addOrEditUser(User user){
        int role = 0;
        if (user.getRole()!= null){
            role = user.getRole().getId();
        }
        Role roles = new Role();
        roles.setId(role);
        user.setRole(roles);
        return  userService.addOrEditUser(user);
    }


    @GetMapping("/delUser")
    public MessageJson delUser(Integer id){
        return userService.delById(id);
    }
    @GetMapping("/selectUserById")
    public MessageJson selectUserById(Integer id){
        User user = userService.selectById(id);
        return user!=null?MessageJson.success(user):MessageJson.error();
    }
    @PostMapping("/delUsers")
    public MessageJson delUsers(String ids){
        // 使用split方法将字符串按逗号分隔
        String[] idArray = ids.split(",");
        // List<String>转换为 List<Integer>
        List<Integer> integerList = new ArrayList<>();
        // 将数组转换为List
        String[] idList = idArray;
        for (String s : idList) {
            integerList.add(Integer.parseInt(s));
        }
        return userService.delIds(integerList);
    }

}
